{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "599ab589-6b5d-4429-9a6b-8ba816a7b227",
   "metadata": {},
   "source": [
    "# Multi-line Labels in Facets\n",
    "\n",
    "\n",
    "The 'newline' character (`\\n`) now works as a 'line break' in the facet titles.</br> \n",
    "Automatic line breaking is performed according to the  text length limit specified with parameters `labWidth` in `facetWrap()` and `xLabWidth`/`yLabWidth` in `facetGrid()`.\n",
    "\n",
    "The margins around the facet titles are controlled by the `margin` parameter in `elementText()`.</br> \n",
    "Horizontal and vertical justifications - using `hjust` and `vjust` parameter in `elementText()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c78e2aa3-1187-4203-b544-4e20989fcddb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"cxW4Xj\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.3.1/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"cxW4Xj\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"cxW4Xj\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "            <div id=\"kotlin_out_0\"></div>\n",
       "            <script type=\"text/javascript\">\n",
       "                            if(!window.kotlinQueues) {\n",
       "                window.kotlinQueues = {};\n",
       "            }\n",
       "            if(!window.kotlinQueues[\"DataFrame\"]) {\n",
       "                var resQueue = [];\n",
       "                window.kotlinQueues[\"DataFrame\"] = resQueue;\n",
       "                window[\"call_DataFrame\"] = function(f) {\n",
       "                    resQueue.push(f);\n",
       "                }\n",
       "            }\n",
       "            (function (){\n",
       "                var modifiers = [(function(script) {\n",
       "    script.src = \"https://cdn.jsdelivr.net/gh/Kotlin/dataframe@3db46ccccaa1291c0627307d64133317f545e6ae/core/src/main/resources/init.js\"\n",
       "    script.type = \"text/javascript\";\n",
       "})];\n",
       "                var e = document.getElementById(\"kotlin_out_0\");\n",
       "                modifiers.forEach(function (gen) {\n",
       "                    var script = document.createElement(\"script\");\n",
       "                    gen(script)\n",
       "                    script.addEventListener(\"load\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {f();};\n",
       "                        window.kotlinQueues[\"DataFrame\"].forEach(function(f) {f();});\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                    }, false);\n",
       "                    script.addEventListener(\"error\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {};\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                        var div = document.createElement(\"div\");\n",
       "                        div.style.color = 'darkred';\n",
       "                        div.textContent = 'Error loading resource DataFrame';\n",
       "                        document.getElementById(\"kotlin_out_0\").appendChild(div);\n",
       "                    }, false);\n",
       "                    \n",
       "                    e.appendChild(script);\n",
       "                });\n",
       "            })();\n",
       "            </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "                <style>\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "                </style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot\n",
    "%use dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "47d3905a-13b3-4c17-aa7a-1eaf46e57d7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.7.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.1."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83b526d2-33ab-445f-813d-8e9cc94edb03",
   "metadata": {},
   "outputs": [],
   "source": [
    "val df = dataFrameOf(\"animal_type\", \"animal\", \"weight\", \"diet\")(\n",
    "    \"pet\", \"cat\", 5, \"carnivore\",\n",
    "    \"pet\", \"dog\", 10, \"carnivore\",\n",
    "    \"pet\", \"rabbit\", 2, \"herbivore\",\n",
    "    \"pet\", \"hamster\", 1, \"herbivore\",\n",
    "\n",
    "    \"farm animal\", \"cow\", 500, \"herbivore\",\n",
    "    \"farm animal\", \"pig\", 100, \"carnivore\",\n",
    "    \"farm animal\", \"horse\", 700, \"herbivore\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0f1f949-c7ce-4e5f-af6e-ff49edbd0200",
   "metadata": {},
   "source": [
    "### Facet Labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f50bf5aa-601b-4aba-b2ad-ef26104cbc0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "val p = letsPlot(df.toMap()) { x = \"animal\"; y = \"weight\" } +\n",
    "    geomBar(stat = Stat.identity) +\n",
    "    themeBW() +\n",
    "    theme(panelGridMinor = elementBlank())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a94fa6c-2a6e-4e2c-9f6f-ebfe69f1798b",
   "metadata": {},
   "source": [
    "#### Default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c30e5c0d-9e18-4d33-ae01-79c3eeb2c9fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"e9tdzC\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm animal\",\"farm animal\",\"farm animal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"ncol\":2.0,\n",
       "\"scales\":\"free\",\n",
       "\"name\":\"wrap\",\n",
       "\"dir\":\"h\",\n",
       "\"facets\":\"animal_type\",\n",
       "\"order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"e9tdzC\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + facetWrap(facets = \"animal_type\", ncol = 2, scales = \"free\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eb94675-7816-4856-8ac1-4d085ae24a81",
   "metadata": {},
   "source": [
    "#### Wrap Labels using `labWidth`-parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ae99612c-c1b0-43ac-8e53-da66d29184a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"4B6XZr\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm animal\",\"farm animal\",\"farm animal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"ncol\":2.0,\n",
       "\"scales\":\"free\",\n",
       "\"name\":\"wrap\",\n",
       "\"dir\":\"h\",\n",
       "\"labwidth\":6.0,\n",
       "\"facets\":\"animal_type\",\n",
       "\"order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"4B6XZr\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + facetWrap(facets = \"animal_type\", ncol = 2, scales = \"free\", labWidth = 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "741e10bd-bd04-4ace-a409-04ba2c3a59bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"TiE8e8\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"diet\":[\"carnivore\",\"carnivore\",\"herbivore\",\"herbivore\",\"herbivore\",\"carnivore\",\"herbivore\"],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm animal\",\"farm animal\",\"farm animal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"x_labwidth\":6.0,\n",
       "\"scales\":\"free\",\n",
       "\"y_order\":1.0,\n",
       "\"name\":\"grid\",\n",
       "\"x\":\"animal_type\",\n",
       "\"y\":\"diet\",\n",
       "\"x_order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"TiE8e8\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\", xLabWidth = 6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbb9d350-f3c3-4d18-9827-f93657b60f60",
   "metadata": {},
   "source": [
    "#### Line Breaks Using `\\n` in Facet Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cb7b453d-7da4-4706-86f7-d952e9c38607",
   "metadata": {},
   "outputs": [],
   "source": [
    "val df2 = df.update { animal_type }.with { it.replace(' ', '\\n') }\n",
    "\n",
    "val p2 = letsPlot(df2.toMap()) { x = \"animal\"; y = \"weight\" } +\n",
    "    geomBar(stat = Stat.identity) +\n",
    "    themeBW() +\n",
    "    theme(panelGridMinor = elementBlank())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c6db2315-cc50-4e69-93aa-90f09fc4da65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"8B4dsp\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"diet\":[\"carnivore\",\"carnivore\",\"herbivore\",\"herbivore\",\"herbivore\",\"carnivore\",\"herbivore\"],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm\\nanimal\",\"farm\\nanimal\",\"farm\\nanimal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"scales\":\"free\",\n",
       "\"y_order\":1.0,\n",
       "\"name\":\"grid\",\n",
       "\"x\":\"animal_type\",\n",
       "\"y\":\"diet\",\n",
       "\"x_order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"8B4dsp\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dbeea0ea-3d60-49a6-a0de-2d3ace1051d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"5avygt\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"diet\":[\"carnivore\",\"carnivore\",\"herbivore\",\"herbivore\",\"herbivore\",\"carnivore\",\"herbivore\"],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm\\nanimal\",\"farm\\nanimal\",\"farm\\nanimal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"ncol\":4.0,\n",
       "\"scales\":\"free\",\n",
       "\"name\":\"wrap\",\n",
       "\"dir\":\"h\",\n",
       "\"facets\":[\"animal_type\",\"diet\"],\n",
       "\"order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"5avygt\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 + facetWrap(facets = listOf(\"animal_type\", \"diet\"), ncol = 4, scales = \"free\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c8bfdd8-cc93-4831-8d64-3eecc13c794b",
   "metadata": {},
   "source": [
    "### Text Justifications"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4d359ce4-1901-478a-bdd9-379b40984027",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Qu8P1w\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"diet\":[\"carnivore\",\"carnivore\",\"herbivore\",\"herbivore\",\"herbivore\",\"carnivore\",\"herbivore\"],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm\\nanimal\",\"farm\\nanimal\",\"farm\\nanimal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"strip_text\":{\n",
       "\"hjust\":1.0,\n",
       "\"vjust\":1.0,\n",
       "\"blank\":false\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"ncol\":4.0,\n",
       "\"scales\":\"free\",\n",
       "\"name\":\"wrap\",\n",
       "\"dir\":\"h\",\n",
       "\"facets\":[\"animal_type\",\"diet\"],\n",
       "\"order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Qu8P1w\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 + facetWrap(facets = listOf(\"animal_type\", \"diet\"), ncol = 4, scales = \"free\") +\n",
    "    theme(stripText = elementText(hjust = 1, vjust = 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7db2d0bc-bf84-4c7e-aaba-9a6c73ee9db6",
   "metadata": {},
   "source": [
    "### Margins Around the Text Element"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "614f608a-aba4-4ecc-b961-81e715330ddf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"LAuGRC\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"animal\",\n",
       "\"y\":\"weight\"\n",
       "},\n",
       "\"data\":{\n",
       "\"animal\":[\"cat\",\"dog\",\"rabbit\",\"hamster\",\"cow\",\"pig\",\"horse\"],\n",
       "\"weight\":[5.0,10.0,2.0,1.0,500.0,100.0,700.0],\n",
       "\"diet\":[\"carnivore\",\"carnivore\",\"herbivore\",\"herbivore\",\"herbivore\",\"carnivore\",\"herbivore\"],\n",
       "\"animal_type\":[\"pet\",\"pet\",\"pet\",\"pet\",\"farm\\nanimal\",\"farm\\nanimal\",\"farm\\nanimal\"]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"stack\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"name\":\"bw\",\n",
       "\"panel_grid_minor\":{\n",
       "\"blank\":true\n",
       "},\n",
       "\"strip_text\":{\n",
       "\"hjust\":0.0,\n",
       "\"vjust\":1.0,\n",
       "\"margin\":[10.0,30.0],\n",
       "\"blank\":false\n",
       "}\n",
       "},\n",
       "\"facet\":{\n",
       "\"scales\":\"free\",\n",
       "\"y_order\":1.0,\n",
       "\"name\":\"grid\",\n",
       "\"x\":\"animal_type\",\n",
       "\"y\":\"diet\",\n",
       "\"x_order\":1.0\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"LAuGRC\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2 + facetGrid(x = \"animal_type\", y = \"diet\", scales = \"free\") +\n",
    "    theme(stripText = elementText(margin = listOf(10, 30), hjust = 0, vjust = 1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
